package com.ruoyi.mall.coupon.service.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.core.to.MemberPriceTo;
import com.ruoyi.common.core.to.SkuReductionTo;
import com.ruoyi.mall.coupon.domain.MemberPrice;
import com.ruoyi.mall.coupon.domain.SkuLadder;
import com.ruoyi.mall.coupon.service.IMemberPriceService;
import com.ruoyi.mall.coupon.service.ISkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.coupon.mapper.SkuFullReductionMapper;
import com.ruoyi.mall.coupon.domain.SkuFullReduction;
import com.ruoyi.mall.coupon.service.ISkuFullReductionService;

/**
 * 商品满减信息Service业务层处理
 *
 * @author zhangqishu
 * @date 2022-06-14
 */
@Service
public class SkuFullReductionServiceImpl implements ISkuFullReductionService {
    @Autowired
    private SkuFullReductionMapper skuFullReductionMapper;

    @Autowired
    private ISkuLadderService skuLadderService;

    @Autowired
    private IMemberPriceService memberPriceService;

    /**
     * 查询商品满减信息
     *
     * @param id 商品满减信息主键
     * @return 商品满减信息
     */
    @Override
    public SkuFullReduction selectSkuFullReductionById(Long id) {
        return skuFullReductionMapper.selectSkuFullReductionById(id);
    }

    /**
     * 查询商品满减信息列表
     *
     * @param skuFullReduction 商品满减信息
     * @return 商品满减信息
     */
    @Override
    public List<SkuFullReduction> selectSkuFullReductionList(SkuFullReduction skuFullReduction) {
        return skuFullReductionMapper.selectSkuFullReductionList(skuFullReduction);
    }

    /**
     * 新增商品满减信息
     *
     * @param skuFullReduction 商品满减信息
     * @return 结果
     */
    @Override
    public int insertSkuFullReduction(SkuFullReduction skuFullReduction) {
        return skuFullReductionMapper.insertSkuFullReduction(skuFullReduction);
    }

    /**
     * 修改商品满减信息
     *
     * @param skuFullReduction 商品满减信息
     * @return 结果
     */
    @Override
    public int updateSkuFullReduction(SkuFullReduction skuFullReduction) {
        return skuFullReductionMapper.updateSkuFullReduction(skuFullReduction);
    }

    /**
     * 批量删除商品满减信息
     *
     * @param ids 需要删除的商品满减信息主键
     * @return 结果
     */
    @Override
    public int deleteSkuFullReductionByIds(Long[] ids) {
        return skuFullReductionMapper.deleteSkuFullReductionByIds(ids);
    }

    /**
     * 删除商品满减信息信息
     *
     * @param id 商品满减信息主键
     * @return 结果
     */
    @Override
    public int deleteSkuFullReductionById(Long id) {
        return skuFullReductionMapper.deleteSkuFullReductionById(id);
    }

    /**
     * 保存 TODO 高级篇再说
     *
     * @param skuReductionTo
     * @return
     */
    @Override
    public int saveSkuReduction(SkuReductionTo skuReductionTo) {
        //1. 保存满减打折，会员价
        SkuLadder skuLadder = new SkuLadder();
        skuLadder.setSkuId(skuReductionTo.getSkuId());
        skuLadder.setFullCount(skuReductionTo.getFullCount());
        skuLadder.setDiscount(skuReductionTo.getDiscount());
        skuLadder.setAddOther(skuReductionTo.getCountStatus());
        if (skuReductionTo.getFullCount() > 0) {
            skuLadderService.insertSkuLadder(skuLadder);
        }

        //2. 保存满减信息
        SkuFullReduction skuFullReduction = new SkuFullReduction();
        BeanUtils.copyProperties(skuReductionTo, skuFullReduction);
        if (skuFullReduction.getFullPrice().compareTo(new BigDecimal("0")) == 1) {
            skuFullReductionMapper.insertSkuFullReduction(skuFullReduction);
        }

        //3. 会员价格
        List<MemberPriceTo> memberPrices = skuReductionTo.getMemberPrices();
        List<MemberPrice> collect = memberPrices.stream().map(item -> {
            MemberPrice memberPrice = new MemberPrice();
            memberPrice.setSkuId(skuReductionTo.getSkuId());
            memberPrice.setMemberLevelId(item.getId());
            memberPrice.setMemberLevelName(item.getName());
            memberPrice.setMemberPrice(item.getPrice());
            memberPrice.setAddOther(1);

            return memberPrice;
        }).filter(item -> item.getMemberPrice().compareTo(new BigDecimal("0")) == 1).collect(Collectors.toList());
        boolean b = memberPriceService.saveBatch(collect);

        return b ? 1 : 0;

    }
}
